package com.gf.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "charaterfilter",urlPatterns = "/*")
public class CharaFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException {
        System.out.println("过滤器工作了");
        HttpServletRequest servletRequest1 = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String uri = servletRequest1.getRequestURI();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        if(!uri.contains(".css") || uri.contains(".js") || uri.contains(".png")||uri.contains(".jpg"))
        {
            servletResponse.setContentType("text/html;charset=utf-8");

        }
        chain.doFilter(servletRequest, servletResponse);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
